<?php
namespace Category\Model;

use Zend\Db\ResultSet\ResultSet;
use Zend\Db\TableGateway\TableGateway;
use Zend\Db\Sql;
use Zend\Db\Sql\Where;
use Zend\Db\Sql\Select;
use Zend\Paginator\Adapter\DbSelect;
use Zend\Paginator\Paginator;
use Zend\Db\Sql\Predicate;
use Zend\Db\Sql\Expression;

class CategoryTable
{
    protected $tableGateway;
	protected $select;
    public function __construct(TableGateway $tableGateway)
    {
        $this->tableGateway = $tableGateway;
		$this->select = new Select();
    }
	public function addCategory($categoryname)
    {			
		$data = array(
			'category_name' 	 => ucfirst($categoryname['cat_name']),					
			'status'           => 1,					
		);	
		if(isset($categoryname['hid_cat']) && $categoryname['hid_cat']!=""){
			$data1 = array(
				'updated_date' 	 => date('Y-m-d H:i:s'),
			 );
			 $data3= array_merge($data, $data1);
			$result=$this->tableGateway->update($data3, array('category_id' => $categoryname['hid_cat']));
		}else{
			 $data1 = array(
				'added_date' 	 => date('Y-m-d H:i:s'),		
				'updated_date' 	 => date('Y-m-d H:i:s'),
			 );
			  $data3=array_merge($data, $data1);
			$result=$this->tableGateway->insert($data3);
		}		
		return $result;
    }
	public function getallCategories(){
		$select = $this->tableGateway->getSql()->select();
		$resultSet = $this->tableGateway->selectWith($select);
		return $resultSet;	        
	} 
    public function getallCategory($categoryId){
	    $select = $this->tableGateway->getSql()->select();
		$select->where('category_id="'.$categoryId.'"');
		$resultSet = $this->tableGateway->selectWith($select);
		return $resultSet->current();	        
	}
	public function deleteCategory($category_id){
		$deleteid=$this->tableGateway->delete(array('(category_id IN ('.$category_id.'))'));
		return $deleteid;
	} 	
	public function checkCategoryName($categoryName){
		$cat_name=ucfirst($categoryName);
		$select = $this->tableGateway->getSql()->select();
		$select->where('category_name="'.$cat_name.'"');
		$resultSet = $this->tableGateway->selectWith($select);
		return $resultSet->count();
	}
	public function getcategorieslistGrid($searchkey,$aColumns,$data,$paginated=false)
	{
		$select = $this->tableGateway->getSql()->select();
		
		// Search
		if(isset($data['sSearch'])){
			if($data['sSearch']!='0' && $data['sSearch']!='')
			{
				$select->where(array(
					new Predicate\PredicateSet(
						array(
						new Predicate\Like('category_name', '%'.$data['sSearch'].'%'),
						),
							Predicate\PredicateSet::COMBINED_BY_OR
					),
				));
			}
		}
		// End		
		// Sort Order
		if(isset($data['iSortCol_0']))
		{
			$sOrder="";
			for($i=0;$i<intval($data['iSortingCols']);$i++)
			{
				if($data['bSortable_'.intval($data['iSortCol_'.$i])] == "true")
				{
					$sOrder.=$aColumns[intval($data['iSortCol_'.$i])]." ".$data['sSortDir_'.$i].",";
				}
			}
			$sOrder = substr_replace($sOrder,"", -1);
		}
		if($sOrder!=''){
			$select->order($sOrder);
		}
		// End		
		// Pagination
		if(isset($data['iDisplayStart']) && $data['iDisplayLength'] != '-1')
		{
			$start=(int)$data['iDisplayStart'];
			$currentPage = (int)$data['iDisplayLength'];
			$resultsPerPage = (int) $currentPage;
			$select->order('category_id DESC')->limit($currentPage)->offset($start);
		}
		// End		
		$resultSet = $this->tableGateway->selectWith($select);
		
		return $resultSet;
	}
	public function getcount($data='')
	{
		$select = $this->tableGateway->getSql()->select();
		if(isset($data['sSearch'])){
			if($data['sSearch']!='0' && $data['sSearch']!='')
			{
				$select->where(array(
					new Predicate\PredicateSet(
						array(
						new Predicate\Like('category_name', '%'.$data['sSearch'].'%'),
						),
							Predicate\PredicateSet::COMBINED_BY_OR
					),
				));
			}
		}
		$resultSet = $this->tableGateway->selectWith($select);		
		return $resultSet->count();
	}
	public function changeStatus($catids,$value)
	{
		$data = array(
			'status'       =>$value,
		);
		$updateStatus=$this->tableGateway->update($data,array('(category_id IN ('.$catids.'))'));
		return 	$updateStatus;			
	}
}